<template>
  <div id="login">
    <!-- 登陆块 -->
    <el-card class="login_box">
      <!-- 标题 -->
      <div class="header">医院后台管理系统</div>

      <!--添加表单-->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login_form" label-width="0px" style="margin-left: 10px;">
        <!--1. 用户名 -->
        <el-form-item prop="username" style="width: 360px;">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-s-custom"></el-input>
        </el-form-item>

        <!--2. 密码 -->
        <el-form-item prop="password" style="width: 360px;">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-unlock" type="password" show-password></el-input>
        </el-form-item>

        <!--3. 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login" style="margin-right: 10px;">登录</el-button>
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      //表单数据
      loginForm: { username: '', password: '' },
      //表单校验
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 8, message: '长度在 5 到 8 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 8, message: '密码为 6~8 位', trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    // 登入
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        console.log(this.loginForm)
        if (!valid) return;
        // 调用get请求,访问后台
        const { data: res } = await this.$http.post('/login', this.loginForm);
        console.log(res)
        if (res.flag === 'ok') {
          this.$message.success('登陆成功！！！');
          //页面路由跳转
          localStorage.setItem('user', JSON.stringify(this.loginForm));  //本地存储
          this.$router.push({ path: '/home' });
        } else {
          this.$message.error('登录失败！！！');
        }
      });
    },
    //注册
    register(){
      this.$router.push({ path: '/register' });
    }
  },
};
</script>

<style lang="less" scoped>
#login {
  height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center/cover;
}
// 登录框
.login_box {
  width: 400px;
  height: 320px;
  padding: 0;
  border-radius: 3px; // 圆角
  position: absolute; // 绝对定位
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%); // 根据自己位置 以自己为长度位移

  /deep/.el-card__body {
    padding: 0;
  }
  
  .header {
    text-align: center; //文字居中
    background-color: #4370b3; //背景色
    color: #fff;
    height: 80px;
    line-height: 80px;
  }

  .btns {
    display: flex; // 弹性布局
    justify-content:space-between; // 尾部对齐
  }

  .login_form {
    position: absolute;
    bottom: 0%;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box; // 设置边框
  }
}
</style>